"""
python p2_legal_solution_first.py
"""

import json
import os

# 读取仓库信息
def read_warehouse_data():
    with open('../fujian/fujian3/origin_data/warehouse.json') as f:
        return json.load(f)

# 读取品类平均库存量
def read_category_inventory():
    with open('../fujian/fujian3/data_from_p1/all_average_inventory.json') as f:
        return {d['category_id']: d['average_inventory'] for d in json.load(f)}

# 读取品类平均销量
def read_category_sales():
    with open('../fujian/fujian3/data_from_p1/all_average_sales.json') as f:
        return {d['category_id']: d['average_sales'] for d in json.load(f)}

warehouses = read_warehouse_data()
category_inventories = read_category_inventory()
category_sales = read_category_sales()

# 按照最大库存量从大到小对仓库排序
sorted_warehouses = sorted(warehouses, key=lambda x: x['max_inventory'], reverse=True)
# 按照库存大小从大到小对品类排序
sorted_categories = sorted(category_inventories.items(), key=lambda x: x[1], reverse=True)

solution = [None] * len(category_inventories)
for category_id, category_inventory in sorted_categories:
    for warehouse in sorted_warehouses:
        if warehouse['max_inventory'] >= category_inventory and warehouse['max_sales'] >= category_sales[category_id]:
            warehouse_index = warehouses.index(warehouse)
            solution[int(category_id)-1] = warehouse_index
            warehouse['max_inventory'] -= category_inventory
            warehouse['max_sales'] -= category_sales[category_id]
            break

# 计算总成本
total_cost = 0
used_warehouses = set()
for category_index, warehouse_index in enumerate(solution):
    if warehouse_index is not None:
        used_warehouses.add(warehouse_index)
        total_cost += warehouses[warehouse_index]['daily_cost']

# 计算仓库使用个数
num_used_warehouses = len(used_warehouses)

# 计算库存量利用率
inventory_utilization = []
for warehouse in warehouses:
    warehouse_id = warehouse['warehouse_id']
    used_inventory = sum([category_inventories[str(i + 1)] for i, w in enumerate(solution) if w == warehouses.index(warehouse)])
    utilization_rate = used_inventory / warehouse['max_inventory'] if warehouse['max_inventory'] > 0 else 0
    inventory_utilization.append({"warehouse_id": warehouse_id, "utilization_rate": utilization_rate})

# 计算出货量利用率
sales_utilization = []
for warehouse in warehouses:
    warehouse_id = warehouse['warehouse_id']
    used_sales = sum([category_sales[str(i + 1)] for i, w in enumerate(solution) if w == warehouses.index(warehouse)])
    utilization_rate = used_sales / warehouse['max_sales'] if warehouse['max_sales'] > 0 else 0
    sales_utilization.append({"warehouse_id": warehouse_id, "utilization_rate": utilization_rate})

# 创建输出文件夹
output_folder = '../fujian/p2/legal_solution_first/'
os.makedirs(output_folder, exist_ok=True)

# 输出总成本到文件
with open(os.path.join(output_folder, 'all_cost.txt'), 'w') as f:
    f.write(str(total_cost))

# 输出仓库使用个数到文件
with open(os.path.join(output_folder, 'num_ware.txt'), 'w') as f:
    f.write(str(num_used_warehouses))

# 输出库存量利用率到JSON文件
with open(os.path.join(output_folder, 'utilization_inventory.json'), 'w') as f:
    json.dump(inventory_utilization, f, indent=4)

# 输出出货量利用率到JSON文件
with open(os.path.join(output_folder, 'utilization_sales.json'), 'w') as f:
    json.dump(sales_utilization, f, indent=4)